package com.sf.dlb.web.controller;


import com.sf.dlb.bean.*;
import com.sf.dlb.constant.ResponseConstant;
import com.sf.dlb.web.service.ChartService;
import com.sf.dlb.utils.DateUtils;
import com.sf.dlb.utils.GsonUtils;
import com.sf.dlb.utils.ResultVOUtil;
import com.sf.dlb.vo.ResultVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


/**
 * 后台图表
 * Created by jing.tan on 2018/12/10.
 */
@RestController
@RequestMapping("/v1/chart/")
public class ChartController extends BaseController{
    private static Logger logger = LoggerFactory.getLogger(ChartController.class);
    @Autowired
    private ChartService chartService;

    /**
     * APP用户基础报表分析
     */
    @PostMapping(value = "/userInfoReport")
    @ResponseBody
    public Object userInfoReport(HttpServletRequest request, @RequestBody UserReportRequestBean
            reqeustData) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        //判断参数是否合法
        if (null==reqeustData) {
            return ResultVOUtil.error(ResponseConstant.ERROR, "参数不合法");
        }
        logger.info("userInfoReport request json === " + GsonUtils.getInstance().toJson(reqeustData));
        if(reqeustData.getTime()==null||reqeustData.getTime().length<2){
            return ResultVOUtil.error(ResponseConstant.ERROR, "请求参数不合法");
        }
        reqeustData.setStartTime(Long.valueOf(reqeustData.getTime()[0]));
        reqeustData.setEndTime(Long.valueOf(reqeustData.getTime()[1]));
        if(reqeustData.getStartTime()<0&&reqeustData.getEndTime()<0){
            return ResultVOUtil.error(ResponseConstant.ERROR, "请求参数不合法");
        }
        reqeustData.setStartTimeD(new Date(reqeustData.getStartTime()));
        reqeustData.setEndTimeD(new Date(reqeustData.getEndTime()));
        Double tempDay= DateUtils.differDay(reqeustData.getStartTimeD(),reqeustData.getEndTimeD());
        if(tempDay< 0){
            return ResultVOUtil.error(ResponseConstant.ERROR, "请求参数不合法");
        }
        if(tempDay> ResponseConstant.user_report_day){
            return ResultVOUtil.error(ResponseConstant.ERROR, "最大统计30天");
        }
        //获取用户userId
        resultVO=chartService.userInfoReport(reqeustData);
        logger.info("userInfoReport response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 1-DLB发放状态-饼图
     */
    @GetMapping(value = "/giveOutDlbPie")
    @ResponseBody
    public Object giveOutDlbPie(HttpServletRequest request) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        resultVO=chartService.giveOutDlbPie();
        logger.info("giveOutDlbPie response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 2-未来30天解锁分布图-折线
     */
    @GetMapping(value = "/unlockDistributionBrokenLine")
    @ResponseBody
    public Object unlockDistributionBrokenLine(HttpServletRequest request) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        resultVO=chartService.unlockDistributionBrokenLine();
        logger.info("unlockDistributionBrokenLine response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }
    /**
     * 3-实名状态-饼图
     */
    @GetMapping(value = "/realStatePie")
    @ResponseBody
    public Object realStatePie(HttpServletRequest request) {
        ResultVO resultVO=(ResultVO)request.getAttribute("token");
        resultVO=chartService.realStatePie();
        logger.info("realStatePie response json === " + GsonUtils.getInstance().toJson(resultVO));
        return resultVO;
    }

}
